package com.open.party.cloud.viewModel;

import android.util.Log
import cn.android.x.model.data.AppPageData
import cn.android.x.model.data.PageReq
import cn.android.x.model.data.ResultInfo
import com.alibaba.fastjson.JSON
import com.open.party.cloud.ServerConfig
import com.open.party.cloud.model.*
import com.open.party.cloud.model.data.Api
import com.sinothk.rxretrofit.RxRetrofit
import org.greenrobot.eventbus.EventBus
import rx.Subscriber
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers


class PartyViewModel {

    // 组织生活》查询分页
    fun getPartyLifePageData(pageReq: PageReq<PartyLifeVo>) {
        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .getPartyLifePageData(pageReq)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<AppPageData<PartyLifeBean>>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val result = ResultInfo<AppPageData<PartyLifeBean>>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    result.eventType = pageReq.data.lifeType.code
                    EventBus.getDefault().post(result)
                }

                override fun onNext(result: ResultInfo<AppPageData<PartyLifeBean>>) {
                    result.eventType = pageReq.data.lifeType.code
                    EventBus.getDefault().post(result)
                }
            })
    }

    fun getPartyLifeDetailById(id: String) {
        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .getPartyLifeDetailById(id)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<PartyLifeBean>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val result = ResultInfo<PartyLifeBean>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    result.eventType = "getPartyLifeDetailById"
                    EventBus.getDefault().post(result)
                }

                override fun onNext(result: ResultInfo<PartyLifeBean>) {
                    result.eventType = "getPartyLifeDetailById"
                    EventBus.getDefault().post(result)
                }
            })
    }

    fun addPartyLife(vo: PartyLifeVo) {

        Log.e("RxRetrofit", JSON.toJSONString(vo))

        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .addPartyLife(vo)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<PartyLifeBean>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val result = ResultInfo<PartyLifeBean>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    result.eventType = "addPartyLife"
                    EventBus.getDefault().post(result)
                }

                override fun onNext(result: ResultInfo<PartyLifeBean>) {
                    result.eventType = "addPartyLife"
                    EventBus.getDefault().post(result)
                }
            })
    }

}
